//
//  HHTitleView.m
//  Flower
//
//  Created by 花花 on 2016/12/5.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHTitleView.h"
@interface HHTitleView() {
    CGSize _titleSize;
    CGFloat _imageHeight;
    CGFloat _imageWidth;
    BOOL _isShowImage;
    
}
@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) UIView *contentView;

@property(nonatomic, strong) UIView *backgroundView;
@end


@implementation HHTitleView

-(instancetype)init{

    self = [self initWithFrame:CGRectZero];
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        self.currentTransformSx = 1.0;
        self.userInteractionEnabled = YES;
        self.backgroundColor  = [UIColor clearColor];
        _isShowImage = NO;
        [self addSubview:self.label];
    }
    return self;
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    if (!_isShowImage) {
        self.label.frame = self.bounds;
    }
    
}

- (CGFloat)titleViewWidth{
    CGFloat width = 0.0f;
    switch (self.imagePostion) {
        case TitleImagePositionLeft:
            width = _imageWidth + _titleSize.width;
            break;
        case TitleImagePositionRight:
            width = _imageWidth + _titleSize.width;
            break;
        case TitleImagePositionTop:
            width = _titleSize.width;
            
            break;
            
        default:
            width = _imageWidth;
            break;
    }
    
    
    return width;
    
}

-(void)adjustSubviewFrame{
    _isShowImage = YES;
    CGRect contentViewFrame = self.bounds;
    contentViewFrame.size.width =[self titleViewWidth];
    contentViewFrame.origin.x = (self.width - contentViewFrame.size.width)/2;
    self.contentView.frame = contentViewFrame;
    self.label.frame = self.contentView.bounds;
    
    [self addSubview:self.contentView];
    [self.label removeFromSuperview];
    
    [self.contentView addSubview:self.label];
    [self.contentView addSubview:self.imageView];
    
    switch (self.imagePostion) {
        case TitleImagePositionTop:{
            CGRect contentViewFrame = self.contentView.frame;
            contentViewFrame.size.height = _imageHeight +_titleSize.height;
            contentViewFrame.origin.y = (self.height -contentViewFrame.size.height)*0.5;
            self.contentView.frame = contentViewFrame;
            
            self.imageView.frame = CGRectMake(0, 0, _imageWidth, _imageHeight);
            CGPoint center = self.imageView.center;
            center.x = self.label.centerX;
            self.imageView.center = center;
            
            CGFloat labelHeight = self.contentView.height - _imageHeight;
            CGRect labelFrame = self.label.frame;
            labelFrame.origin.y = _imageHeight;
            labelFrame.size.height = labelHeight;
            self.label.frame = labelFrame;
            
            break;
            
        }
           
        case TitleImagePositionLeft:{
            
            CGRect labelFrame = self.label.frame;
            labelFrame.origin.x = _imageWidth;
            labelFrame.size.width = self.contentView.frame.size.width - _imageWidth;
            self.label.frame = labelFrame;
            
            CGRect imageFrame = CGRectZero;
            imageFrame.size.height = _imageHeight;
            imageFrame.size.width = _imageWidth;
            imageFrame.origin.y = (self.contentView.frame.size.height - imageFrame.size.height)/2;
            self.imageView.frame = imageFrame;
                break;
        }
        

        case TitleImagePositionRight:{
            CGRect labelFrame = self.label.frame;
            labelFrame.size.width = self.contentView.frame.size.width - _imageWidth;
            self.label.frame = labelFrame;
            
            CGRect imageFrame = CGRectZero;
            imageFrame.origin.x = CGRectGetMaxX(self.label.frame);
            imageFrame.size.height = _imageHeight;
            imageFrame.size.width = _imageWidth;
            imageFrame.origin.y = (self.contentView.frame.size.height - imageFrame.size.height)/2;
            self.imageView.frame = imageFrame;

        
                break;
        }
            
        case TitleImagePositionCenter:
            self.imageView.frame = self.contentView.bounds;
            [self.label removeFromSuperview];
            break;
        default:
            break;
    }
    
    
}
#pragma mark - Setter && Getter
-(void)setCurrentTransformSx:(CGFloat)currentTransformSx{

    _currentTransformSx = currentTransformSx;
    self.transform = CGAffineTransformMakeScale(currentTransformSx, currentTransformSx);

}

-(void)setNormalImage:(UIImage *)normalImage{

    _normalImage = normalImage;
    _imageWidth = normalImage.size.width;
    _imageHeight = normalImage.size.height;
    self.imageView.image = normalImage;

}
-(void)setSelectedImage:(UIImage *)selectedImage{

    _selectedImage = selectedImage;
    self.imageView.highlightedImage = selectedImage;

}
-(void)setFont:(UIFont *)font{

    _font = font;
    self.label.font = font;
}
-(void)setText:(NSString *)text{
    _text = text;
    self.label.text = text;
    
    CGRect bouns = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];

    _titleSize = bouns.size;
}
-(void)setTextColor:(UIColor *)textColor{

    _textColor = textColor;
    self.label.textColor = textColor;

}

-(void)setSelected:(BOOL)selected{

    _selected = selected;
    self.imageView.highlighted = selected;

}


-(UIImageView *)imageView{
    
    if (!_imageView) {
        _imageView = [[UIImageView  alloc]init];
        _imageView.contentMode = UIViewContentModeCenter;
    }
    
    return _imageView;
    
}

-(UILabel *)label{
    
    if (!_label) {
        _label = [[UILabel  alloc]init];
        _label.textAlignment = NSTextAlignmentCenter;
    }
    
    return _label;
    
}


-(UIView *)contentView{
    
    if (!_contentView) {
        _contentView = [[UIView  alloc]init];
    }
    
    return _contentView;
    
}


@end
